-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adiciona possibilidade de uso com MongoDB #278
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revisei apenas a parte de integração (ou seja, ainda não olhei para a implementação do MongoDB em si, db/mongo.go
e seus testes), mas muito bom! Feliz demais com essa colaboração!
Deixei alguns comentários, tem coisas que ainda não precisamos mudar antes de dar merge, mas tá muito bom!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mais comentários, ainda sem chegar no módulo do MongoDB, ainda focando na integração : )
cmd/api.go
Outdated
uri := os.Getenv("DATABASE_URL") | ||
if strings.HasPrefix(uri, "mongodb://") { | ||
mdb, _ := db.NewMongoDB(mongoDatabase) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer mdb.Close() | ||
api.Serve(&mdb, port, nr) | ||
|
||
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") { | ||
pg, err := db.NewPostgreSQL(u, postgresSchema, nr) | ||
if err != nil { | ||
return err | ||
} | ||
defer pg.Close() | ||
|
||
api.Serve(&pg, port, nr) | ||
} else { | ||
return fmt.Errorf("unsupported database URI, must start with postgres:// or mongodb://") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bem melhor : ) Mas ainda dá para evitar repetição usando a interface, algo como:
var db database
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") {
db = postgres{DATABASE_URL}
} else if strings.HasPrefix(uri, "mongodb://") {
db = mongoDb{DATABASE_URL}
} else {
// ...
}
err := api.Server(db)
if err != nil {
fmt.Println("Error:", err)
}
Fiz um exemplo executável se ajudar: https://go.dev/play/p/RAYi89lY1oi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cuducos eu ainda fico meio confuso com a integraçaão do golang com interfaces, entao eu por enquanto estou estudando mais essa minha deficiencia para retormar. enquanto isso tirei as linhas vazias.
vou retomar essa estrutura mais enxuta depois dos estudos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E, finalmente tive tempo de revisar o mongodb.go
— deixei alguns comentários, nada complicado, está bem bom!
Caso inglês seja uma barreira, deixe uns // TODO: translate
q eu dou um tapa depois!
cmd/api.go
Outdated
|
||
uri := os.Getenv("DATABASE_URL") | ||
if strings.HasPrefix(uri, "mongodb://") { | ||
mdb, _ := db.NewMongoDB(uri) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer mdb.Close() | ||
api.Serve(&mdb, port, nr) | ||
|
||
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") { | ||
pg, err := db.NewPostgreSQL(u, postgresSchema, nr) | ||
if err != nil { | ||
return err | ||
} | ||
defer pg.Close() | ||
|
||
api.Serve(&pg, port, nr) | ||
} else { | ||
return fmt.Errorf("unsupported database URI, must start with postgres:// or mongodb://") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa implementação precisa mudar usando a interface, para evitar repetição.
cmd/cmd.go
Outdated
if strings.HasPrefix(uri, "mongodb://") { | ||
mdb, err := db.NewMongoDB(uri) | ||
if err != nil { | ||
return err | ||
} | ||
err = mdb.CreateCollection() | ||
if err != nil { | ||
return err | ||
} | ||
err = mdb.CreateIndexes() | ||
if err != nil { | ||
return err | ||
} | ||
defer mdb.Close() | ||
return err | ||
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") { | ||
pg, err := db.NewPostgreSQL(u, postgresSchema, nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer pg.Close() | ||
return pg.CreateTable() | ||
} else { | ||
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa implementação precisa mudar usando a interface, para evitar repetição.
cmd/cmd.go
Outdated
uri := os.Getenv("DATABASE_URL") | ||
if strings.HasPrefix(uri, "mongodb://") { | ||
mdb, err := db.NewMongoDB(uri) | ||
if err != nil { | ||
return err | ||
} | ||
err = mdb.DropCollection() | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} else if strings.HasPrefix(uri, "postgres://") || strings.HasPrefix(uri, "postgresql://") { | ||
pg, err := db.NewPostgreSQL(u, postgresSchema, nil) | ||
if err != nil { | ||
return err | ||
} | ||
defer pg.Close() | ||
return pg.DropTable() | ||
} else { | ||
return fmt.Errorf("A URL não contém 'mongodb' nem 'postgres'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa implementação precisa mudar usando a interface, para evitar repetição.
cmd/transform.go
Outdated
if cleanUp { | ||
if err := pg.DropTable(); err != nil { | ||
uri := os.Getenv("DATABASE_URL") | ||
if strings.HasPrefix(uri, "mongodb://") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa implementação precisa mudar usando a interface, para evitar repetição.
.github/workflows/tests.yaml
Outdated
- name: Iniciar MongoDB no Linux | ||
if: runner.os == 'Linux' | ||
run: | | ||
docker run -d --name mongodb -p 27017:27017 \ | ||
-e MONGO_INITDB_ROOT_USERNAME=minhareceita \ | ||
-e MONGO_INITDB_ROOT_PASSWORD=minhareceita \ | ||
-e MONGO_INITDB_DATABASE=minhareceita \ | ||
--health-cmd "mongosh -u minhareceita -p minhareceita --authenticationDatabase admin --eval 'db.runCommand(\"ping\")'" \ | ||
--health-interval 10s --health-timeout 5s --health-retries 5 \ | ||
mongo:8 | ||
|
||
|
||
- name: Iniciar MongoDB no Windows | ||
if: runner.os == 'Windows' | ||
run: | | ||
docker run -d --name mongodb -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=minhareceita -e MONGO_INITDB_ROOT_PASSWORD=minhareceita -e MONGO_INITDB_DATABASE=minhareceita mongo:8 | ||
|
||
- name: Instalar MongoDB Shell no Windows | ||
if: runner.os == 'Windows' | ||
run: | | ||
Invoke-WebRequest -Uri "https://downloads.mongodb.com/compass/mongosh-2.3.9-win32-x64.zip" -OutFile "mongosh.zip" | ||
Expand-Archive -Path mongosh.zip -DestinationPath "C:\mongosh" | ||
echo "C:\mongosh" | Out-File -Append -Encoding ASCII $env:GITHUB_PATH | ||
|
||
- name: Esperar MongoDB estar pronto no Windows | ||
if: runner.os == 'Windows' | ||
run: | | ||
docker ps | ||
exit 1 | ||
shell: pwsh | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa lógica está desnecessariamente complexa. Não precisamos rodar o MondoDB dentro da máquina em que rodam os testes.
Essa é a arquitetura atual:
architecture-beta
group c[Test container]
service mr(server)[Minha Receita] in c
service db(database)[Postgres]
mr:R -- L:db
O desejável é ter algo como:
architecture-beta
group c[Test container]
service mr(server)[Minha Receita] in c
service db(database)[Postgres]
service m(database)[MongoDB]
mr:R -- L:db
mr:L -- R:m
O que você está fazendo é:
architecture-beta
group c[Test container]
service mr(server)[Minha Receita] in c
service db(database)[Postgres]
service m(database)[MongoDB] in c
mr:R -- L:db
mr:L -- R:m
Isso traz toda a complexidade de saber como rodar o MongoDB dependendo do sistema operacional em que os testes estão rodando. Isso não faz sentido. O MongoDB é um serviço independente, e deve ser rodado em um container independente, como um serviço. E aí a gente não se preocupa em como rodar o MongoDb.
Existem duas maneiras de fazez isso.
- Como você mesmo sugeriu, usando uma Action pronta (vc suegriu essa, mas talvez seja melhor uma mais popular como essa)
- Como eu sugeri, usando containers de serviço do GitHub Actions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
essa popular n roda no windows, eu testei ela. vou de container
Adicionado o uso do MongoDB no projeto. Ainda falta terminar as documentações
See #72